package cn.yeoman.flowcontrol.core.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.yeoman.flowcontrol.configurable.OutPutService;
import cn.yeoman.flowcontrol.core.FlowControl;
import cn.yeoman.flowcontrol.model.Notice;
import cn.yeoman.flowcontrol.service.DispatcherStation;

@Service("flowControl")
public class FlowControlImpl implements FlowControl {

	@Resource(name = "dispatcherStation")
	private DispatcherStation dispatcherStation;
	
	@Resource(name = "outPutService")
	private OutPutService outPutService;
	
	@Override
	public boolean startProcess(String affairNo, String typeCode) {
		return dispatcherStation.startProcess(affairNo, typeCode);
	}

	@Override
	public Map<String, Character> getMultiAffairStatus(
			HashSet<String> affairNos, String typeCode) {
		return dispatcherStation.getMultiAffairStatus(dispatcherStation.replaceContent(affairNos, typeCode));
	}

	@Override
	public boolean cancelControl(String affairNo, String typeCode) {
		return dispatcherStation.deleteProcessInstance(dispatcherStation.generateInstanceNo(affairNo, typeCode));
	}

	@Override
	public List<Notice> takeOutNoticesByAffairType(String typeCode) {
		if(StringUtils.isNotBlank(typeCode)){
			List<Notice> noticeList = new ArrayList<Notice>();
			List<Notice> returnList = new ArrayList<Notice>();
			noticeList.addAll(outPutService.getNotices());
			if(noticeList != null && !noticeList.isEmpty()){
				for(Notice notice : noticeList)
					if(notice.getTypeCode().equals(typeCode))
						returnList.add(outPutService.give(notice));
			}
			return returnList.isEmpty() ? null : returnList;
		}
		return null;
	}

}
